<template>
  <div>
  </div>
</template>

<script>
export default {
  data: function () {
    return this.gdata.data
  },
  mounted () {
    // 初始化
    this.init()
  },
  methods: {
    init: function () {
      if (typeof (WebSocket) === 'undefined') {
        alert('您的浏览器不支持socket')
      } else {
        // 实例化socket
        this.wssocket = new WebSocket(this.wsuri)
        // 监听socket连接
        this.wssocket.onopen = this.open
        // 监听socket错误信息
        this.wssocket.onerror = this.error
        // 监听socket消息
        this.wssocket.onmessage = this.getmessage
      }
    },
    open: function () {
      this.jxdebug('socket连接成功')
    },
    error: function () {
      this.jxdebug('连接错误')
    },
    getmessage: function (wsmsg) {
      var rsp = JSON.parse(wsmsg.data)
      this.$parent.G2C(rsp)
      this.jxdebug('收到' + wsmsg.data)
    },
    send: function (params) {
      this.wssocket.send(params)
    },
    close: function () {
      this.jxpop('socket已经关闭')
    }
  },
  destroyed () {
    // 销毁监听
    this.wssocket.onclose = this.close
  }
}
</script>

<style>
</style>
